/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: kfa_writer.h
 *
 * Purpose: implementation writer to write data to kfa file
 *
 * Developer:
 *   wen.gu , 2022-03-06
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __KFA_WRITER_H__
#define __KFA_WRITER_H__
#include <string>
#include <vector>
#include <stdio.h>
#include "kfa_types.h"
#include "kfa_mux.h"
#include "kfa_encoder.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/

struct KfaWriterInitializeParam
{
    std::string file_name;    
    kfa_header header;
};
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

class KfaWrtier final
{

public:
    KfaWrtier();
    ~KfaWrtier();
public:
     
    bool initialize(const KfaWriterInitializeParam& param, bool is_simulation);

    /** the file_size just for debug 
     *  is_simulation: false: really write to kfa file, true: just recorde output size 
     */
    bool write(const uint8_t* data, uint32_t size, bool is_simulation, uint32_t file_size = 0);

    uint32_t total_out_size() const {return total_out_size_;}

    bool sync(bool is_simulation); /** write cached data in writer to file */

    bool is_initialized() const { return is_initialized_; }

private:
    bool setOutputFile(const std::string& file_name);
    bool doWrite(const uint8_t* data, uint32_t size, bool is_simulation);
private:
    bool is_initialized_ = false;
    uint32_t img_width_ = 0;
    uint32_t img_height_ = 0;
    uint32_t total_out_size_ = 0;
    uint32_t total_file_size_ = 0;

    bool is_err_occur_ = false;
    uint32_t buf_idx_ = 0;

    KfaMux muxer_;
    KfaEncoder* encoder_ = nullptr;
};




#endif /** !__KFA_WRITER_H__ */

